package com.redrose.audioandvideo.utils;

import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;


/**
 * Toast 工具类
 */

public class ToastUtil {
    private static Toast mToast;

    //要传application 的context
    public static void show(final Context context, final String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
                } else {
                    mToast.setText(str);
                }
                mToast.setGravity(Gravity.BOTTOM, 0, 250);
                mToast.show();
            }
        };
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            runnable.run();
        } else {
            ThreadPoolUtil.runOnMainThread(runnable);
        }
    }
}
